var ctx;
var cellInfoId = "";
var cellRegionId = "";
var cellBuildingId = "";
var cellUnitId = "";
var buildingId;
var unitId;
var phoneReg = /^(0?1[358]\d{9})$|^((0(10|2[1-3]|[3-9]\d{2}))?[1-9]\d{6,7})$/;
var strP=/^\d+(\.\d+)?$/;

function setCtx(ctx) {
	this.ctx = ctx;
	initListener();  //添加区域监听事件
}

$(function(){
	buildingByRegion();
	unitByBuilding();
})

//省市区级联
function initListener() {
    $("#cellRegionId").change(function () {
    	buildingByRegion();
    });
    
    $("#cellBuildingId").change(function () {
    	unitByBuilding();
    });
    
    $('#cellBuildingId').change(function() {
    	if ($('#cellBuildingId').val() != 0) {
    		cellBuildingId = $('#cellBuildingId').val();
    	}
    });
}

function buildingByRegion(){
	cellRegionId = $('#cellRegionId').val();
	buildingId = $('#buildingId').val();
	if (cellRegionId != ''){
		$.ajax({
	        type : "GET",
	        url : ctx + "/cellHouse/getCellBuildingByRegionId",
	        cache : false,
	        dataType : 'json',
	        data : {
	        	regionId : cellRegionId
	        },
	        success : function(data) {
	        	if (data.success) {
	        		var result = "<option value='" + "'>--楼栋--</option>";
	        		$.each(data.obj, function(index, item) {
	                	if(item.id == buildingId){
	                		result += "<option id='" + item.id + "'" +" selected='selected'" + " value='" + item.id + "'>" + item.buildingName + "</option>";
	                	}else{
	                		result += "<option id='" + item.id + "'" + " value='" + item.id + "'>" + item.buildingName + "</option>";
	                	}
	                });
	                $("#cellBuildingId").html('');
	                $("#cellBuildingId").append(result);
	                $("#cellBuildingId").change();
	            }
	        }
		});
	} else {
		var result = "<option value='" + "'>--楼栋--</option>";
		$("#cellBuildingId").html('');
        $("#cellBuildingId").append(result);
        $("#cellBuildingId").change();
	}
}

function unitByBuilding(){
	cellBuildingId = $('#cellBuildingId').val();
	unitId = $('#unitId').val();
	if (cellBuildingId != ''){
		$.ajax({
	        type : "GET",
	        url : ctx + "/cellHouse/getCellUnitByBuildingId",
	        cache : false,
	        dataType : 'json',
	        data : {
	        	buildingId : cellBuildingId
	        },
	        success : function(data) {
	        	if (data.success) {
	        		var result = "<option value='" + "'>--单元--</option>";
	                $.each(data.obj, function(index, item) {
	                    if(item.id == unitId){
	                		result += "<option id='" + item.id + "'" +" selected='selected'" + " value='" + item.id + "'>" + item.name + "</option>";
	                	}else{
	                		result += "<option id='" + item.id + "'" + " value='" + item.id + "'>" + item.name + "</option>";
	                	}
	                });
	                $("#cellUnitId").html('');
	                $("#cellUnitId").append(result);
	            }
	        }
		});
	} else {
		var result = "<option value='" + "'>--单元--</option>";
		$("#cellUnitId").html('');
        $("#cellUnitId").append(result);
	}
}

//验证房屋名称
function checkName(){
	var name = $("#name").val();
	//解决ie9以下placeholder与value重复的问题
	if(name == $("#name").attr("placeholder")){
		name = "";
	}
	
	if(name == null || name == ""){
		layer.tips('房屋名称不能为空!', '#name', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}

//验证区域不为空
function checkCellRegion(){
	var regionId = $("#cellRegionId").val();
	if(regionId == null || regionId == ""){
		layer.tips('请选择区域!', '#cellRegionId', {
			tips: 3,
		    time:1000
		});
	return false;
	}
	return true;
}

//验证楼栋不为空
function checkCellBuilding(){
	var buildingId = $("#cellBuildingId").val();
	
	if(buildingId == null || buildingId == ""){
		layer.tips('请选择楼栋!', '#cellBuildingId', {
			tips: 3,
		    time:1000
		});
		return false;
	}
	return true;
}

//验证单元不为空
function checkCellUnit(){
	var cellUnitId = $("#cellUnitId").val();
	
	if(cellUnitId == null || cellUnitId == ""){
		layer.tips('请选择单元!', '#cellUnitId', {
			tips: 3,
		    time:1000
		});
		return false;
	}
	return true;
}

//验证房屋类型
function checkHouseType(){
	var houseType = $("#houseType").val();
	
	if(houseType == null || houseType == ""){
		layer.tips('请至少选择一种房屋类型!', '#houseType', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}

//验证房屋状态
function checkHouseState(){
	var houseState = $("#houseState").val();
	if(houseState == null || houseState == ""){
		layer.tips('请至少选择一种房屋状态!', '#houseState', {
			tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}

//验证房屋面积
function checkArea(){
	var area = $("#area").val();
	//解决ie9以下placeholder与value重复的问题
	if(area == $("#area").attr("placeholder")){
		area = "";
	}
	if(area == null || area == ""){
		return true;
	}else if(!strP.test(area)){	
		layer.tips('面积只能是数字或小数!', '#area', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{

		return true;
	}
}

//验证物业费单价
function checkPerPrice(){
	var perPrice = $("#perPrice").val();
	//解决ie9以下placeholder与value重复的问题
	if(perPrice == $("#perPrice").attr("placeholder")){
		perPrice = "";
	}
	if(perPrice == null || perPrice == ""){
		return true;
	}else if(!strP.test(perPrice)){	
		layer.tips('单价只能是数字或小数!', '#perPrice', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{

		return true;
	}
}

//验证排序
function checkSort(){
	var sort = $("#sort").val();
	var reg = /^[0-9]*[1-9][0-9]*$/;
	if(sort == null || sort == ""){
		layer.tips('排序不能为空!', '#sort', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else if(!reg.test(sort)){
		layer.tips('请输入正整数!', '#sort', {
			tips: 3,
			time:1000
		});
		return false;
	}else{
		return true;
	}
}

//验证手机
function checkPhone(){
	var phone = $("#ownerPhone1").val();
	//解决ie9以下placeholder与value重复的问题
	if(phone == $("#ownerPhone1").attr("placeholder")){
		phone = "";
	}
	if(phone == null || phone == ""){
		return true;
	}else{
		if(!phoneReg.test(phone)){
			layer.tips('电话号码格式错误，请填写正确格式的电话号码!', '#ownerPhone1', {
			    tips: 3,
			    time:1000
			});
			return false;
		}else{
			return true;
		}
	}
		
}

//验证电话号码2
function checkPhone1(){
	var phone1 = $("#ownerPhone2").val();
	//解决ie9以下placeholder与value重复的问题
	if(phone1 == $("#ownerPhone2").attr("placeholder")){
		phone1 = "";
	}
	if(phone1 == null || phone1 == ""){
		return true;
	}else if(!phoneReg.test(phone1)){
		layer.tips('电话号码格式错误，请填写正确格式的电话号码!', '#ownerPhone2', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}

//保存或新增方法
function saveCellHouse() {
	var name = $('#name').val();
	var houseType = $('#houseType').val();
	var houseState = $('#houseState').val();
	var area = $('#area').val();
	var perPrice = $('#perPrice').val();
	/*var sort = $('#sort').val();*/
	var ownerName1 = $('#ownerName1').val();
	var ownerPhone1 = $('#ownerPhone1').val();
	var ownerName2 = $('#ownerName2').val();
	var ownerPhone2 = $('#ownerPhone2').val();
	cellRegionId = $('#cellRegionId').val();
	cellBuildingId = $('#cellBuildingId').val();
	cellUnitId = $('#cellUnitId').val();
	if($('#houseId').val() != "" && $('#houseId').val() != undefined){
		houseId = $('#houseId').val();
	}else{
		houseId = 0;
	}
	
	if(checkName() && checkCellRegion() && checkCellBuilding() && checkCellUnit()
			&& checkHouseType() && checkHouseState() && checkArea() && checkPerPrice() && checkPhone()/* && checkSort()*/){
		var index1 = layer.load(1);
		$.ajax({
			type : "POST",
			url : ctx + "/cellHouse/saveOrUpdateCellHouse",
			cache : false,
			dataType : 'json',
			data: {
				name : name,
				regionId : cellRegionId,
				buildingId : cellBuildingId,
				unitId : cellUnitId,
				houseType : houseType,
				houseState : houseState,
				area : area,
				perPrice : perPrice,
				houseId :houseId,
				/*sort : sort,*/
				ownerName1 :ownerName1,
				ownerPhone1 : ownerPhone1,
				ownerName2 : ownerName2,
				ownerPhone2 : ownerPhone2
			},
			success : function(result) {
				if (result.success) {
					layer.msg("操作成功",{
						icon: 1,
						time: 3000 //2秒关闭（如果不配置，默认是3秒）
					},function(){
						loadContent("/cellHouse/cellHouseList");
					});
				} else {
					layer.close(index1);
					layer.msg(result.msg,{
						icon: 2
					});
				}
			}
		});
	}

			
}

//返回上一页
function cancel(){
	parent.loadContent("/cellHouse/cellHouseList");
}
